
package com.bear.cmcc.NetTestHTTP.util;

import com.bear.cmcc.NetTestHTTP.component.VillageBean;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityGridData;

import java.util.Comparator;

@SuppressWarnings("rawtypes")
public class SortCellStringSpeed implements Comparator {
    public final static int UP = 1;

    public final static int DOWN = -1;

    private int state;

    /**
     * @param state 排序状态值
     */
    public SortCellStringSpeed(int state) {
        this.state = state;
    }

    public int compare(Object obj1, Object obj2) {
        if (obj1 instanceof VillageBean) {
            VillageBean ps1 = (VillageBean) obj1;
            VillageBean ps2 = (VillageBean) obj2;
            if (state == SortCellStringSpeed.DOWN) {
                return sortDonw(ps1.averageSpeed, ps2.averageSpeed);
            }
            return sortUp(ps1.averageSpeed, ps2.averageSpeed);
        } else if (obj1 instanceof CityGridData) {
            CityGridData ps1 = (CityGridData) obj1;
            CityGridData ps2 = (CityGridData) obj2;
            if (state == SortCellStringSpeed.DOWN) {
                return sortDonw(Double.valueOf(ps1.getAverageSpeed()),
                        Double.valueOf(ps2.getAverageSpeed()));
            }
            return sortUp(Double.valueOf(ps1.getAverageSpeed()),
                    Double.valueOf(ps2.getAverageSpeed()));
        }
        return 0;

    }

    /**
     * 升序
     * 
     * @param sp1
     * @param sp2
     * @return
     */
    private int sortUp(double sp1, double sp2) {
        Double d1 = 0.0, d2 = 0.0;
        try {
            d1 = sp1;
            d2 = sp2;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (d1.compareTo(d2) < 0)
            return -1;
        else if (d1.compareTo(d2) > 0)
            return 1;
        else
            return 0;
    }

    /**
     * 降序
     * 
     * @param sp1
     * @param sp2
     * @return
     */
    private int sortDonw(double sp1, double sp2) {
        Double d1 = 0.0, d2 = 0.0;
        try {
            d1 = sp1;
            d2 = sp2;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (d1.compareTo(d2) > 0)
            return -1;
        else if (d1.compareTo(d2) < 0)
            return 1;
        else
            return 0;
    }

}
